//
//  DSXDetailViewController.m
//  iDerbyOSSampler
//
//  Created by glenn andreas on 1/7/14.
//  Copyright (c) 2014 glenn andreas. All rights reserved.
//

#import "DSXDetailViewController.h"
#import "DerbyNSOde.h"
#import "DerbyBoutState.h"
#import "DerbyStopwatch.h"
#import "DXMLBoutObject.h"
#import "DXMLRuleSetObject.h"
@interface DSXDetailViewController ()
@end

@implementation DSXDetailViewController

#pragma mark - Managing the detail item


- (void)viewDidLoad
{
    self.binders = [NSMutableArray array];
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


@implementation DSXScoreboardViewController
- (void) viewDidLoad
{
    [super viewDidLoad];
    DXMLRuleSetObject *ruleSet = self.nsode.xml.ruleSet;
    self.nsode.boutState.jamTimer = [[DerbyStopwatch alloc] initWithMaximumTime:ruleSet.jam countsUp:NO];
    self.nsode.boutState.periodTimer = [[DerbyStopwatch alloc] initWithMaximumTime:ruleSet.period countsUp:NO];
    self.nsode.boutState.lineupTimer = [[DerbyStopwatch alloc] initWithMaximumTime:ruleSet.lineup countsUp:YES];
    self.nsode.boutState.timeoutTimer = [[DerbyStopwatch alloc] initWithMaximumTime:ruleSet.timeout countsUp:YES];
    self.nsode.boutState.intermissionTimer = [[DerbyStopwatch alloc] initWithMaximumTime:12 * 60 * 60 countsUp:NO];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.jamTimeLabel bindToObject:self.nsode.boutState.jamTimer keyPath:@"timeString"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.periodTimeLabel bindToObject:self.nsode.boutState.periodTimer keyPath:@"timeString"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.lineupTimeLabel bindToObject:self.nsode.boutState.lineupTimer keyPath:@"timeString"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.timeoutTimeLabel bindToObject:self.nsode.boutState.timeoutTimer keyPath:@"timeString"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.jamNumberLabel bindToObject:self.nsode.boutState keyPath:@"currentJam.number"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.periodNumberLabel bindToObject:self.nsode.boutState keyPath:@"currentPeriod.number"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.homeScoreLabel bindToObject:self.nsode.boutState keyPath:@"homeTeamState.score"]];
    [self.binders addObject:[[DSXLabelBinder alloc] initWithLabel:self.awayScoreLabel bindToObject:self.nsode.boutState keyPath:@"awayTeamState.score"]];
}
@end

@interface DSXBinder()
@property (strong, nonatomic) id object;
@property (strong, nonatomic) NSString *keypath;
@end
@implementation DSXBinder
- (void) dealloc
{
    [_object removeObserver:self forKeyPath:_keypath];
}
- (id) initBindToObject: (id) object keyPath: (NSString *) path
{
    self = [super init];
    if (self) {
        _object = object;
        _keypath = path;
        [object addObserver:self forKeyPath:path options:0 context:NULL];
    }
    return self;
}
- (void) updateBindingValue
{
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    [self updateBindingValue];
}
@end
@interface DSXLabelBinder()
@property (strong, nonatomic) UILabel *label;
@end
@implementation DSXLabelBinder
- (id) initWithLabel: (UILabel *) label bindToObject: (id) object keyPath: (NSString *) path
{
    self = [super initBindToObject:object keyPath:path];
    if (self) {
        _label = label;
        [self updateBindingValue];
    }
    return self;
}
- (void) updateBindingValue
{
    [self.label setText:[[self.object valueForKeyPath:self.keypath] description]];
}
@end
